/*
 * @Author: your name
 * @Date: 2021-08-27 09:04:50
 * @LastEditTime: 2021-08-29 16:39:07
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \briup\5-javascript\day05\1-函数调用.js
 */

//函数声明
function add(a, b) {
    return a + b;
}

//函数表达式
var sayName = function() {
    console.log(this.name);
}

//文件内的全局范围内被调用
console.log(add(1, 2)); //3
sayName(); //undefined   global.name 

//在对象内，对象成员
var p1 = {
    name: 'zs',
    sayName
}
p1.sayName();


//函数名.call(执行环境对象，实参列表);
//函数名.apply(执行环境对象，实参列表数组);
//执行环境对象：函数作为对象中的成员

var p2 = {
    name: 'lisi'
}
sayName.call(p2); //'lisi'
sayName.apply(p2); //'lisi'
sayName.call(this); // undefined  this指代node 执行环境

//call与apply的功能与区别
//功能：反建立函数与对象之间的关系
function print(color1, color2) {
    console.log(this.price, color1, color2);
}
print('red', 'blue'); //undefined red blue
print.call(this, 'red', 'blue', 'green'); //undefined red blue
print.apply(this, ['red', 'blue', 'green']); //undefined red blue

var p1 = { name: '连衣裙', price: 100 };
var p2 = { name: '笔记本', price: 2000, print };
print.call(p1, 'red', 'green'); //100 red green
print.apply(p1, ['red', 'green']); //100 red green
p2.print('red'); //2000 red undefined
print.call(p2, 'red'); //2000 red undefined
print.apply(p2, ['red']); //200 red undefined